﻿
using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Logic;

namespace YouHP.Pages.TreatmentHistory
{
    public class HistoryStop : PatientBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();

        }

        protected TreatmentHistoryInfo TrmHistoryData;
        protected TreatmentInfo TrmData;
        protected PrescriptionInfo PresInfo;
        protected PrescriptionHistoryInfo PresHistInfo;
        protected int bCurrentInfo = 0;
        protected int _treamentOrPrescriptionId = 0;
        protected int _type = 0;

        protected string _Name;
        protected string _StartDataString;

        protected new void HandleCommon()
        {
            this.AddLocation("治疗记录", "/TreatmentHistory/Index.aspx");
            this.AddLocation("停止治疗", "/TreatmentHistory/HistoryStop.aspx");
            this.SetTitle("停止治疗");

            bCurrentInfo = RequestTools.GetInt("bCur", 0);
            int trmid = RequestTools.GetQueryInt("trmid", 0);
            int type = RequestTools.GetQueryInt("type", 0);

            _treamentOrPrescriptionId = trmid;
            _type = type;

            if (0 == type)
            {
                TrmData = TreatmentBLL.Instance.GetTreatmentInfo(trmid);
                if (TrmData.Id <= 0)
                {
                    this.AddErrLine("治疗不存在");
                    return;
                }

                TreatmentHistoryLogic trmHisLogic = new TreatmentHistoryLogic();
                TrmHistoryData = trmHisLogic.GetCurrentTreatment(PrData.Id, trmid);
                if (TrmHistoryData.Id <= 0)
                {
                    this.AddErrLine("无效的疾病历史信息");
                    return;
                }
                if (TrmHistoryData.PRId != PrData.Id)   // 攻击
                {
                    this.AddErrLine("无效的疾病历史信息");
                    return;
                }

                _Name = string.Format("{0} - {1}", TrmData.Name, TrmData.GeneralName);
                _StartDataString = TrmHistoryData.StartDate.ToString("yyyy-MM-dd");

            }
            else if (1 == type)
            {
                PresInfo = PrescriptionBLL.Instance.GetPrescriptionInfo(trmid);
                if (PresInfo.Id <= 0)
                {
                    this.AddErrLine("中药方不存在");
                    return;
                }

                PresHistInfo = PatientPrescriptionBLL.Instance.GetCurrentPrescription(PrData.Id, trmid);
                if (PresHistInfo.Id <= 0)
                {
                    this.AddErrLine("无效的疾病历史信息");
                    return;
                }

                _Name = string.Format("{0}", PresInfo.Name);
                _StartDataString = PresHistInfo.StartDate.ToString("yyyy-MM-dd");
            }
            else
            {
                this.AddErrLine("无效的治疗类型");
            }
 
        }

        protected new void HandlePost()
        {
            if (0 == _type)
            {
                TreatmentHistoryInfo info = ValidateForm();
                if (info == null || ErrorCount > 0)
                    return;

                bool succ = PatientTreatmentBLL.Instance.StopTreatmentRecorde(info);
                if (succ)
                {
                    //添加动态
                    UserActionInfo _UserAction = new UserActionInfo();
                    _UserAction.UserId = UserData.Id;
                    _UserAction.UserName = UserData.UserName;
                    _UserAction.ActionTime = DateTime.Now;
                    _UserAction.ActionType = Constants.ACTION_TREATMENT_STOP;
                    _UserAction.ActionContent = "&nbsp;<a href=\"/Pages/Alpha/Treatment.aspx?id=" + info.TreatmentId + "\"target=\"_blank\">" + TrmData.Name + "</a><font color=red>(" + Constants.TreatmentStopReason[info.StopReason] +")</font>&nbsp;";
                    UserActionBLL.Instance.AddUserAction(_UserAction);

                    this.AddMsgLine("修改成功");
                }
                else
                {
                    this.AddMsgLine("修改失败");
                }
                this.SetUrl("/TreatmentHistory/List.aspx?trmid=" + TrmData.Id);
                this.SetMetaRefresh();
            }

            else if (1 == _type)
            {
                PrescriptionHistoryInfo info = ValidateFormOfPrescription();
                if (info == null || ErrorCount > 0)
                    return;

                bool result = PatientPrescriptionBLL.Instance.StopPrescriptionHistory(PrData.Id, _treamentOrPrescriptionId, info.EndDate);
                if (result)
                {

                    this.AddMsgLine("修改成功");
                }
                else
                {
                    this.AddMsgLine("修改失败");
                }
                this.SetUrl("/TreatmentHistory/Index.aspx");
                this.SetMetaRefresh();
            }

        }

        protected new  void HandleGet()
        {
            this.AddDatePicker();
        }

        protected TreatmentHistoryInfo ValidateForm()
        {
            TreatmentHistoryInfo info = new TreatmentHistoryInfo();

            // 对时间进行判断
            string value = RequestTools.GetFormString("enddate");
            if (value == "")
            {
                this.AddErrLine("请填写停止时间");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }
            info.StartDate = date;  //作为另一条记录的起始时间
            info.EndDate = date;

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("停止日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            //else if (DateTime.Compare(date, PrData.StartDate) < 0)
            //{
            //    this.AddErrLine("停止日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
            //    return null;
            //}

            //对时间进行检查
            if (bCurrentInfo > 0 && info.StartDate < TrmHistoryData.StartDate)
            {
                this.AddErrLine("停止使用时间不能比开始使用时间早！");
                return null;
            }


            info.StopReason = RequestTools.GetFormInt("stopreason", -1);
            if (info.StopReason >= Constants.TreatmentStopReason.Length || info.StopReason < 0)
            {
                this.AddErrLine("请填写停止原因");
                return null;
            }

            info.UpdateDate = DateTime.Now;
            info.TreatmentId = TrmData.Id;
            info.State = Constants.STOPED_USE;
            info.IsUsing = 0;
            info.PRId = PrData.Id;
            
            return info;

        }

        protected PrescriptionHistoryInfo ValidateFormOfPrescription()
        {
            PrescriptionHistoryInfo info = PatientPrescriptionBLL.Instance.GetCurrentPrescription(PrData.Id, _treamentOrPrescriptionId);

            // 对时间进行判断
            string value = RequestTools.GetString("enddate");
            if (value == "")
            {
                this.AddErrLine("请填写停止时间");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }
            info.StartDate = date;  //作为另一条记录的起始时间
            info.EndDate = date;

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("停止日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            else if (DateTime.Compare(date, PrData.StartDate) < 0)
            {
                this.AddErrLine("停止日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
                return null;
            }

            //对时间进行检查
            if (bCurrentInfo > 0 && info.StartDate < PresHistInfo.StartDate)
            {
                this.AddErrLine("停止使用时间不能比开始使用时间早！");
                return null;
            }


            info.StopReason = RequestTools.GetFormInt("stopreason", -1);
            if (info.StopReason >= Constants.TreatmentStopReason.Length || info.StopReason < 0)
            {
                this.AddErrLine("请填写停止原因");
                return null;
            }

            info.UpdateDate = DateTime.Now;
            info.State = Constants.STOPED_USE;

            return info;
        }
    }
}